<?php


namespace app\common\model;


use app\store\model\store\shop\Coach;

class Fitness extends BaseModel
{
    protected $name = "user_fitness";

    public function shop()
    {
        $module = static::getCalledModule() ?: 'common';
        return $this->BelongsTo("app\\{$module}\\model\\store\\Shop");
    }
    public function coach()
    {
        $module = static::getCalledModule() ?: 'common';
        return $this->BelongsTo("app\\{$module}\\model\\Coach");
    }
    /**
     * 关联用户表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        $module = static::getCalledModule() ?: 'common';
        return $this->BelongsTo("app\\{$module}\\model\\User");
    }

    public static function detail($where)
    {
        return static::get($where,['shop','coach','user']);
    }
    public function getDataAttr($value){
        return htmlspecialchars_decode($value);
    }

    public function getList($param=[])
    {
        $params = array_merge([
            'type'=>0,              //类型
            'coach_id'=>0,           //教练
            'user_id' => 0,         // 搜索关键词
            'listRows' => 15,       // 每页数量
            'search'=>'',            //搜索内容
            'date'=>'',              //搜索日期
            'id'=>''                //搜索id
        ], $param);
        $filter = [];
        $params['type'] > 0 && $filter['type'] = $params['type'];
        $param['type'] == 1 && $filter['type'] = ['in','1,6'];
        $params['coach_id'] > 0 && $filter['coach_id'] = $params['coach_id'];
        $params['user_id'] > 0 && $filter['user_id'] = $params['user_id'];
        $params['search'] != '' && $filter['title'] = $params['search'];
        $params['date'] != '' && $filter['date'] = $params['date']; //日期
        $params['id'] != '' && $filter['id'] = $params['id']; //搜索阶段

        // 获取列表数据
        return $this->with(['shop','user','coach'])->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params['listRows'], false, [
                'query' => request()->request()
            ]);
    }

    /**
     * @title 获取教练和阶段
     * @param
     * @author Qian
     * Time: 2023/3/29  10:32
     */
    public function getCoachAndStage($param){
        $params = array_merge([
            'type'=>0, //类型
            'search'=>'', //搜索内容
            'user_id'=>0, //用户
            'date'=>''
        ], $param);
        $filter = [];$stage = [];
        $params['user_id'] > 0 && $filter['user_id'] = $params['user_id'];
        $params['type'] > 0 && $filter['type'] = $params['type'];
        $params['search'] != '' && $filter['title'] = $params['search'];
        // 获取列表数据
        $coach = $this->with(['coach'])->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['id' => 'desc'])->group('coach_id')->group('coach_id')->select();
        if ($params['type'] == 2){
            $stage = $this->where($filter)
                ->where('is_delete', '=', 0)
                ->order(['id' => 'desc'])->field('id,title')->select();
        }

        return ['coach'=>$coach,'stage'=>$stage];
    }

    public function add($data,$user_id,$type){
        if ($data['coach_id'] == ''){
            $this->error = '请选择教练';
            return false;
        }
        if ($data['title'] == ''){
            $this->error = '请输入表头';
            return false;
        }
        $coach = Coach::detail(['coach_id'=>$data['coach_id']]);//教练信息
        $json_data = json_encode($data['data'],true);
        $add['title'] = $data['title'];
        $add['date'] = $data['date'];
        $add['user_id'] = $user_id;
        $add['coach_id'] = $data['coach_id'];
        $add['shop_id'] = $coach['shop_id'];
        $add['data'] = $json_data;
        $add['type'] = $type;//计划表
        $add['wxapp_id'] = self::$wxapp_id;
        if ($this->allowField(true)->save($add)){
            return true;
        }else{
            return  false;
        }
    }
    
    public function addAll($data,$user_id,$type){
        if ($data['coach_id'] == ''){
            $this->error = '请选择教练';
            return false;
        }
        if ($data['title'] == ''){
            $this->error = '请输入表头';
            return false;
        }
        $coach = Coach::detail(['coach_id'=>$data['coach_id']]);
        if ($type == 2){
            //阶段表
            $all = array_map(function ($item) use ($user_id,$coach) {
                return [
                    'user_id' => $user_id,
                    'coach_id' => $coach['coach_id'],
                    'shop_id' => $coach['shop_id'],
                    'data' => json_encode($item,true),
                    'wxapp_id' => self::$wxapp_id
                ];
            }, $data);
            if ($this->allowField(true)->save($all)){
                return true;
            }else{
                return  false;
            }
        }else{
            $json_data = json_encode($data['data'],true);
            $add['user_id'] = $user_id;
            $add['coach_id'] = $data['coach_id'];
            $add['shop_id'] = $coach['shop_id'];
            $add['data'] = $json_data;
            $add['type'] = $type;//计划表
            $add['wxapp_id'] = self::$wxapp_id;
            if ($this->allowField(true)->save($add)){
                return true;
            }else{
                return  false;
            }
        }
    }

    public function edit($data){
//        if ($data['coach_id'] == ''){
//            $this->error = '请选择教练';
//            return false;
//        }
        if ($data['title'] == ''){
            $this->error = '请输入表头';
            return false;
        }
        $json_data = json_encode($data['data'],true);
        $add['title'] = $data['title'];
        $add['date'] = $data['date'];
        $add['data'] = $json_data;
        if ($this->allowField(true)->save($add)){
            return true;
        }else{
            return  false;
        }
    }

    public function setDelete(){
        return $this->save(['is_delete'=>1]);
    }

}